<?php
/**
 * 货币释放功能.
 * User: madong
 * Date: 2018/10/16
 * Time: 1:27 AM
 */

namespace app\api\controller\jiaoyi;


use think\Controller;
use think\Db;
use think\Request;

class Coinshifang extends Controller
{
    /**
     * 释放货币
     *
     * 参数
     *
     * @id  当前订单id
     *
     * @uordernumber  当前订单号
     *
     * @oordernumber  对方订单号
     */
    public function Coin_shifang (){

        $res = Request::instance();

        $Order_all_info = Db::name('jiaoyibuylog')->where('ordernumber',$res->param('uordernumber'))->find();

        $pass = Db::name('user')->where('uname',$res->param('uname'))->value('secpwd');

        if (!$pass){echo json_encode(['zt'=>0,'msg'=>'您还没有设置支付密码']);die();}

        if (!$res->param('pass') == $pass){echo json_encode(['zt'=>0,'msg'=>'支付密码错误']);die();}

        if ($Order_all_info['oorderzt'] == 2){echo json_encode(['zt'=>0,'msg'=>'您已释放过了']);die();}

        if ($res->isPost()){

            if ($res->param('type') == 1){//订单

                Db::startTrans();

                try{

                    Db::name('jiaoyibuylog')->where('id',$res->param('id'))->where('ordernumber',$res->param('uordernumber'))->update([

                        'oorderzt' => 2,//已释放状态

                        'qrsktime' => time(),//释放时间

                    ]);

                    Db::name('jiaoyi')->where('ordernumber',$res->param('oordernumber'))->setInc('jiaoyiok',1);//广告商当前订单交易成功加1

                    Db::name('jiaoyi')->where('ordernumber',$res->param('oordernumber'))->setInc('jiaoyics',1);//广告商当前订单交易成功加1

                    Db::name('jiaoyi')->where('ordernumber',$res->param('oordernumber'))->setInc('xinren',1);//广告商当前订单信任加1

                    Db::name('jiaoyi')->where('ordernumber',$res->param('oordernumber'))->setDec('yfkdd',1);//广告商已付款减去1

                    Db::name('jiaoyi_uinfo')->where('uname',$Order_all_info['oname'])->setInc('xinren',1);//用户表信任加1

                    Db::name('jiaoyi_uinfo')->where('uname',$Order_all_info['oname'])->setInc('jiaoyics',1);//用户表交易次数加1

                    Db::name('jiaoyi_uinfo')->where('uname',$Order_all_info['oname'])->setInc($Order_all_info['ubizhong'],$Order_all_info['ucoinnum']);//货币增加（求购方）

                    Db::name('jiaoyi_uinfo')->where('uname',$Order_all_info['uname'])->setDec('d'.$Order_all_info['ubizhong'],$Order_all_info['ucoinnum']);//冻结数减少（出售方）

                    Db::name('jiaoyidj')->where('ddhao',$res->param('uordernumber'))->update([

                        'djzt' => 1,

                        'sftime' => time(),

                        'djztgb' => '用户'.$Order_all_info['uname'].'冻结的'.$Order_all_info['ubizhong'].'释放给用户'.$Order_all_info['oname'].'--'.$Order_all_info['ucoinnum'].'个'

                    ]);


                    Db::commit();

                    echo json_encode(['zt'=>1,'msg'=>'释放成功本次交易完成','data'=>1]);

                } catch (\Exception $e) {

                    echo json_encode(['zt'=>0,'msg'=>'释放失败!']);

                    Db::rollback();
                }

            }elseif ($res->param('type') == 0){//广告

                Db::startTrans();

                try{

                    Db::name('jiaoyibuylog')->where('id',$res->param('id'))->where('ordernumber',$res->param('uordernumber'))->update([

                        'oorderzt' => 2,//已释放状态

                        'qrsktime' => time(),//释放时间

                    ]);

                    Db::name('jiaoyi')->where('ordernumber',$res->param('oordernumber'))->setInc('jiaoyiok',1);//广告商当前订单交易成功加1

                    Db::name('jiaoyi')->where('ordernumber',$res->param('oordernumber'))->setInc('jiaoyics',1);//广告商当前订单交易成功加1

                    Db::name('jiaoyi')->where('ordernumber',$res->param('oordernumber'))->setInc('xinren',1);//广告商当前订单信任加1

                    Db::name('jiaoyi')->where('ordernumber',$res->param('oordernumber'))->setDec('yfkdd',1);//广告商已付款减去1

                    Db::name('jiaoyi_uinfo')->where('uname',$Order_all_info['uname'])->setInc('xinren',1);//用户表信任加1

                    Db::name('jiaoyi_uinfo')->where('uname',$Order_all_info['uname'])->setInc('jiaoyics',1);//用户表交易次数加1

                    Db::name('jiaoyi_uinfo')->where('uname',$Order_all_info['uname'])->setInc($Order_all_info['ubizhong'],$Order_all_info['ucoinnum']);//货币增加（求购方）

                    Db::name('jiaoyi_uinfo')->where('uname',$Order_all_info['oname'])->setDec('d'.$Order_all_info['ubizhong'],$Order_all_info['ucoinnum']);//冻结数减少（出售方）

                    Db::name('jiaoyidj')->where('ddhao',$res->param('uordernumber'))->update([

                        'djzt' => 1,

                        'sftime' => time(),

                        'djztgb' => '用户'.$Order_all_info['oname'].'冻结的'.$Order_all_info['ubizhong'].'释放给用户'.$Order_all_info['uname'].'--'.$Order_all_info['ucoinnum'].'个'

                    ]);


                    Db::commit();

                    echo json_encode(['zt'=>1,'msg'=>'释放成功本次交易完成','data'=>1]);

                } catch (\Exception $e) {

                    echo json_encode(['zt'=>0,'msg'=>'释放失败!']);

                    Db::rollback();
                }

            }else{

                echo json_encode(['zt'=>0,'msg'=>'恶意访问']);

            }



        }else{

            if (Db::name('eyiip')->insert([

                'ip'=>\request()->ip(),

                'note'=>'错误不是POST请求 - 文件路径/jiaoyi/Coinshifang 方法名：Coin_shifang',

                'time'=>time()

            ])){

                echo json_decode(['zt'=>1,'msg'=>'(^_^)']);

            }else{

                echo json_decode(['zt'=>0,'msg'=>'Error']);

            }

        }

    }

}